# -*- coding: utf-8 -*-
# @Time    : 2025/1/22 下午4:52
# @Author  : lh
# @Email   : lhhlhh822@peatlmail.com
# @File    : __init__.py.py
# @Software: PyCharm

from pages.base_page import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select


# 添加部门和岗位页面类
class OrganizationChartPage(BasePage):
    url = '/crm/index.php?m=user'
    Add_department_locator = (By.LINK_TEXT, '添加部门')  # 点击添加部门按钮
    department_locator = (By.XPATH, '/html/body/div[8]/div[2]/form/div[1]/div/input')  # 输入名字
    superior_department_locator = (By.NAME, 'parent_id')  # 点击上下级
    department_description_locator = (By.CSS_SELECTOR, '#department_add > div:nth-child(3) > div > textarea')  # 描述
    affirm_locator = (By.CSS_SELECTOR, 'body > div:nth-child(9) > div.ui-dialog-buttonpane.ui-widget-content.ui-helper'
                                       '-clearfix > div > button:nth-child(1)')

    add_post_locator = (By.CSS_SELECTOR, '#add_role')
    name_post_locator = (By.CSS_SELECTOR, '#role_add > div:nth-child(1) > div > input')
    bm_locator = (By.NAME, 'department_id')
    ms_post_locator = (By.CSS_SELECTOR, '#role_add > div:nth-child(4) > div > textarea')
    qd_post_locator = (By.CSS_SELECTOR,
                       'body > div:nth-child(10) > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > '
                       'div > button:nth-child(1)')
    result_locator = (By.XPATH, '/html/body/div[5]/div[2]')
    result_add_locator = (By.XPATH, '/html/body/div[5]/div[2]/div/div[1]')

    add_user_locator = (By.LINK_TEXT, '添加用户')
    name_user_locator = (By.XPATH, '/html/body/div[5]/div[2]/div/div/div[2]/form/table/tbody/tr[1]/td[2]/input')
    password_locator = (By.NAME, 'password')
    user_category_locator = (By.XPATH, '/html/body/div[5]/div[2]/div/div/div[2]/form/table/tbody/tr[3]/td[2]/select')
    department_user_locator = (By.XPATH, '/html/body/div[5]/div[2]/div/div/div[2]/form/table/tbody/tr[4]/td[2]/select')

    post_user_locator = (By.XPATH, '/html/body/div[5]/div[2]/div/div/div[2]/form/table/tbody/tr[5]/td[2]/select')

    # user_category_locator = (By.XPATH, '/html/body/div[5]/div[2]/div/div/div[2]/form/table/tbody/tr[3]/td[
    # 2]/select') department_user_locator = (By.XPATH, '/html/body/div[5]/div[2]/div/div/div[2]/form/table/tbody/tr[
    # 4]/td[2]/select') post_user_locator = (By.ID, 'role2') add_user_locator = (By.XPATH, '/html/body/div[5]/div[
    # 2]/div/div/div[2]/form/table/tbody/tr[6]/td[2]/input[1]')

    tj_user_locator = (By.XPATH, '/html/body/div[5]/div[2]/div/div/div[2]/form/table/tbody/tr[6]/td[2]/input[1]')

    #点击添加部门按钮
    def click_add_department(self):
        self.find_element(self.Add_department_locator).click()

    #输入部门名
    def input_department(self, department):
        self.find_element(self.department_locator).send_keys(department)

    def click_superior_department(self):
        self.find_element(self.superior_department_locator).click()

    #输入描述内容
    def input_department_description(self, description):
        self.find_element(self.department_description_locator).send_keys(description)

    #添加部门确定按钮
    def click_affirm(self):
        self.find_element(self.affirm_locator).click()

    #点击添加岗位按钮
    def click_add_post(self):
        self.find_element(self.add_post_locator).click()

    #输入岗位名
    def input_name_post(self, name_post):
        self.find_element(self.name_post_locator).send_keys(name_post)

    #选择所属部门
    def drop_bm(self, film):
        bm_element = Select(self.find_element(self.bm_locator))
        bm_element.select_by_index(film)

    #输入描述内容
    def input_ms_post(self, ms_post):
        self.find_element(self.ms_post_locator).send_keys(ms_post)

    #添加岗位确认按钮
    def click_qd_past(self):
        self.find_element(self.qd_post_locator).click()

    #点击添加用户按钮
    def add_user(self):
        self.find_element(self.add_user_locator).click()

    #输入用户名
    def input_name_user(self, name):
        self.find_element(self.name_user_locator).send_keys(name)

    #输入密码
    def input_password(self, password):
        self.find_element(self.password_locator).send_keys(password)

    #选择类别
    def category_user(self, category):
        select = Select(self.find_element(self.user_category_locator))
        select.select_by_index(category)

    #选择部门
    def department_user(self, departments):
        bm = Select(self.find_element(self.department_user_locator))
        bm.select_by_index(departments)
        # self.find_element(self.bm_locator).click()

    #选择岗位
    def post_user(self, post):
        select = Select(self.find_element(self.post_user_locator))
        select.select_by_index(post)

    #点击添加
    def tj_user(self):
        self.find_element(self.tj_user_locator).click()

    def get_result_tips(self):
        return self.find_element(self.result_locator).text

    def get_add_result_tips(self):
        return self.find_element(self.result_add_locator).text
